package com.cehome.cloudbox.utils;

import com.cehome.cloudbox.constans.ExtensionTaskProperties;
import com.cehome.cloudbox.doman.vo.TaskInfo;
import liquibase.util.StringUtils;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;

import java.util.ArrayList;
import java.util.List;

/**
 * 手动工作流----工具类
 * @author zfl
 */
public class FlowAbleUtil {

    /**
     * 任务节点-组
     *
     * @param id 任务id
     * @param name 任务名称
     * @param candidateGroup 用户组
     * @return UserTask
     */
    public static UserTask createGroupTask(String id, String name, String candidateGroup) {
        List<String> candidateGroups = new ArrayList<>();
        candidateGroups.add(candidateGroup);
        UserTask userTask = new UserTask();
        userTask.setDocumentation("描述");
        userTask.setName(name);
        userTask.setId(id);
        userTask.setCandidateGroups(candidateGroups);
        userTask.setAssignee("approver");

        //会签
        // 配置会签所需条件.
        MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = new MultiInstanceLoopCharacteristics();
        // 配置节点人员办理顺序 串行:true 并行:false.
        multiInstanceLoopCharacteristics.setSequential(false);
        // 配置会签集合变量名称.
        multiInstanceLoopCharacteristics.setInputDataItem("${assigneeList}");
        // 配置会签集合遍历名称.
        multiInstanceLoopCharacteristics.setElementVariable("assignee");
        // 将下一节点元素转换为任务节点对象.
        // 设置下一节点处理人表达式 引用会签条件activiti:elementVariable="assignee".
        userTask.setAssignee("${assignee}");
        // 下一任务节点设置会签循环特征.
        userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);

        //
        return userTask;
    }

    /**
     * 任务节点用户
     *
     * @param id 任务id
     * @param name 任务名称
     * @param userPkNo 候选人
     * @return UserTask
     */
    public static UserTask createUserTask(String id, String name, String userPkNo) {
        List<String> candidateUsers = new ArrayList<String>();
        candidateUsers.add(userPkNo);
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        userTask.setCandidateUsers(candidateUsers);
        return userTask;
    }

    /**
     * 任务节点-锁定者
     *
     * @param id 任务id
     * @param name 任务名称
     * @param assignee 受理人
     * @return UserTask
     */
    public UserTask createAssigneeTask(String id, String name, String assignee) {
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        userTask.setAssignee(assignee);
        return userTask;
    }

    /**
     * 连线
     *
     * @param from 来源-----节点id
     * @param to 到达-----节点id
     * @param name 线-名称
     * @param conditionExpression 条件表达式
     * @return SequenceFlow
     */
    public static SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        flow.setName(name);
        if (StringUtils.isNotEmpty(conditionExpression)) {
            flow.setConditionExpression(conditionExpression);
        }
        return flow;
    }


    /**
     * 排他网关
     * @param id 网关id
     * @param name 网关名称
     * @return ExclusiveGateway
     */
    public static ExclusiveGateway createExclusiveGateway(String id, String name) {
        ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
        exclusiveGateway.setId(id);
        exclusiveGateway.setName(name);
        return exclusiveGateway;
    }

    /**
     * 并行网关
     * @param id 网关id
     * @param name 网关名称
     * @return ParallelGateway
     */
    public static ParallelGateway createParallelGateway(String id, String name) {
        ParallelGateway gateway = new ParallelGateway();
        gateway.setId(id);
        gateway.setName(name);
        return gateway;
    }

    /**
     * 开始节点
     * @return StartEvent
     */
    public static FlowElement createStartEvent(){
        StartEvent event = new StartEvent();
        event.setId("startEvent_1");
        event.setName("开始");
        return event;
    }

    /**
     * 结束节点
     * @return EndEvent
     */
    public static FlowElement createEndEvent() {
        EndEvent event = new EndEvent();
        event.setId("endEvent_1");
        event.setName("结束");
        return event;
    }


    public static Process createProcess(List<TaskInfo> list,String key){
        Process process = new Process();
        process.setId(key);
        process.setName("process_1");
        FlowElement startFlowElement= createStartEvent();
        process.addFlowElement(startFlowElement);

      /*  UserTask shenqingUserTask=new UserTask();
        shenqingUserTask.setId("task_"+UUidUtil.getUUID());
        shenqingUserTask.setName("申请");
        shenqingUserTask.setAssignee("${"+ExtensionTaskProperties.FLOWABLE_APPLY_USER_ID+"}");
        process.addFlowElement(shenqingUserTask);*/
        FlowElement endFlowElement=createEndEvent();


        List<FlowElement> userTaskList = new ArrayList<>();

        for(int i = 0; i <list.size(); i++) {
            String firstName = "huiqian";
            String nextParam = "nextParam";
            if(i != 0){
                firstName = "huiqian"+i;
            }
            UserTask userTask=new UserTask();
            userTask.setId("task_"+UUidUtil.getUUID());
            userTask.setName("审批"+i);
            if(i==0){
                //这里是定死了第一个申请 节点的候选人站位符
                userTask.setAssignee("${"+ExtensionTaskProperties.FLOWABLE_APPLY_USER_ID+"}");
            }else{
                userTask.setAssignee("${"+ExtensionTaskProperties.FLOWABLE_ASSIGNEE+i+"}");
            }

            List<CustomProperty> customProperties = new ArrayList<>();
            CustomProperty customPropertyp = new CustomProperty();
            customPropertyp.setName(userTask.getId()+nextParam);
            customPropertyp.setSimpleValue("huiqian"+(i+1));
            customProperties.add(customPropertyp);


            CustomProperty customProperty = new CustomProperty();
            customProperty.setName(userTask.getId()+"shenpi");
            customProperty.setSimpleValue(list.get(i).getAssgins());
            customProperties.add(customProperty);
            if(list.get(i).isZhihui()){
                CustomProperty customProperty1 = new CustomProperty();
                customProperty1.setName(userTask.getId()+"zhihui");
                customProperty1.setSimpleValue(list.get(i).getChaosongs());
                customProperties.add(customProperty1);
            }
            if(list.get(i).isHuiqian()){
                //全做成会签形式，但是要让使用者不知道，所以还要增加自定义参数，是否是单选
                CustomProperty customPropertyd = new CustomProperty();
                customPropertyd.setName(userTask.getId()+"isHuiqian");
                //是单选存true，否则false
                customPropertyd.setSimpleValue(list.get(i).isHuiqian()+"");
                customProperties.add(customPropertyd);

                CustomProperty customProperty1 = new CustomProperty();
                customProperty1.setName(userTask.getId()+"huiqian");
                customProperty1.setSimpleValue(list.get(i).getAssgins());
                customProperties.add(customProperty1);

                userTask.setCustomProperties(customProperties);
                MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics=new MultiInstanceLoopCharacteristics();
                //并行
                multiInstanceLoopCharacteristics.setSequential(false);
                //完成条件,默认所有人都完成
                multiInstanceLoopCharacteristics.setCompletionCondition("${nrOfActiveInstances == nrOfInstances}");
                //元素变量多实例,一般和设置的assignee变量是对应的
                multiInstanceLoopCharacteristics.setElementVariable(ExtensionTaskProperties.FLOWABLE_ASSIGNEE+i);
                //集合多实例,用于接收集合数据的表达式
                multiInstanceLoopCharacteristics.setInputDataItem("${"+firstName+"}");
                userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);
            }else{
                if(i != 0){
                    userTask.setAssignee("${"+firstName+"}");
                }
                userTask.setCustomProperties(customProperties);
            }
            process.addFlowElement(userTask);
            userTaskList.add(userTask);
        }

//        userTaskList.add(0,shenqingUserTask);
        process.addFlowElement(endFlowElement);
        //连线-----
        //如果只有一个节点。那么开始---节点----结束就OK了
        List<SequenceFlow> sequenceFlowList = new ArrayList<>();
        SequenceFlow startSequenceFlow=createSequenceFlow(startFlowElement.getId(),userTaskList.get(0).getId(),"sequence_"+UUidUtil.getUUID(),"");
        process.addFlowElement(startSequenceFlow);

        SequenceFlow endSequenceFlow=createSequenceFlow(userTaskList.get(userTaskList.size()-1).getId(),endFlowElement.getId(),"sequence_"+UUidUtil.getUUID(),"");
        process.addFlowElement(endSequenceFlow);
        if(userTaskList.size()!=1){
            for(int i = 0; i < userTaskList.size()-1; i++){
                SequenceFlow sequenceFlow=createSequenceFlow(userTaskList.get(i).getId(),userTaskList.get(i+1).getId(),"sequence_"+UUidUtil.getUUID(),"");
                process.addFlowElement(sequenceFlow);
            }
        }
        return process;
    }
}
